<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.conflictsResolve_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/ninebasic/dispute/conflictsResolve";
  import {getMultilevelMore} from "@/api/system/multilevelDict";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {
          "eventCategory": ["01", "0101"]
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 1050,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          excelBtn: true,
          dialogClickModal: false,
          column: [
            {
              label: "所属网格",
              prop: "gridId",
              type: "tree",
              span: 24,
              parent: false,
              display: true,
              dicMethod: "get",
              dicUrl: "/api/smart-system/dept/tree",
              props: {
                label: "title"
              
              },
              checkStrictly: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入所属网格",
                trigger: "click"
              }],
            },
            {
              label: "事件名称",
              prop: "eventName",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入事件名称",
                trigger: "blur"
              }],
            },
            // {
            //   label: "事件编号",
            //   prop: "eventNo",
            //   type: "input",
            //   hide: true,
            // },
            {
              label: "发生日期",
              prop: "happenDate",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              rules: [{
                required: true,
                message: "请输入发生日期",
                trigger: "blur"
              }],
            },
            {
              label: "发生地点",
              prop: "happenPlace",
              type: "input",
              rules: [{
                required: true,
                message: "请输入发生地点",
                trigger: "blur"
              }],
            },
            {
              label: "事件规模",
              prop: "eventScale",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=event_scale",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              rules: [{
                required: true,
                message: "请输入事件规模",
                trigger: "blur"
              }],
            },
            // {
            //   label: "事件类别",
            //   prop: "eventCategory",
            //   type: "select",
            //   dicUrl: "/api/smart-system/dict/dictionary?code=eventCategory",
            //   props: {
            //     label: "dictValue",
            //     value: "dictKey"
            //   },
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入事件类别",
            //     trigger: "blur"
            //   }],
            // },
            // {
            //   label: "事件类别",
            //   prop: "eventCategory",
            //   type: "cascader",
             
            //   props: {
            //   label: 'name',
            //   value: 'code'
            //   },
            // lazy: true,
            // lazyLoad(node, resolve) {
            //   console.log("000000000000000");
            //   let stop_level = 1;
            //   let level = node.level;
            //   let data = node.data || {}
            //   let code = data.code;
            //   let list = [];
            //   let callback = () => {
            //     console.log("callback");
            //     resolve((list || []).map(ele => {
            //       return Object.assign(ele, {
            //         leaf: level >= stop_level
            //       })
            //     }));
            //   }
            //   if (level == 0) {
            //     console.log("--------------2222------------");
            //     getMultilevel("event_type","0","1").then(res => {
            //       list = res.data.data;
            //       callback()
            //     })
            //   }else if (level == 1) {
            //     console.log("----------------2222----------");
            //     getMultilevel("event_type",code,"2").then(res => {
            //       list = res.data.data;
            //       callback()
            //     })
            //   }
            // }
            // },
            {
            label: '事件类别',
            prop: 'eventCategory',
            type: "cascader",
            dicData: [],
            filterable: true,
            hide: true,
            rules: [{
              required: true,
              message: "请输入事件类别",
              trigger: "blur"
            }],
            },
            {
              label: "涉及人数",
              prop: "numberInvolved",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入涉及人数",
                trigger: "blur"
              }],
            },
            {
              label: "事件简述",
              prop: "eventDescription",
              type: "textarea",
              minRows:1,
              maxlength: 4000,
              showWordLimit: true,
              hide: true,
              span: 24,
              rules: [{
                required: true,
                message: "请输入事件简述",
                trigger: "blur"
              }],
            },
            {
              label: "涉及单位",
              prop: "relatedUnits",
              type: "input",
              hide: true,
            },
             {
              label: "主要当事人姓名",
              prop: "name",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入主要当事人姓名",
                trigger: "blur"
              }],
            },
            {
              label: "当事人证件",
              prop: "identificationCode",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=certificate_code",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入当事人证件",
                trigger: "blur"
              }],
            },
            {
              label: "当事人证件号码",
              prop: "idCard",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入当事人证件号码",
                trigger: "blur"
              }],
            },
           
            {
              label: "当事人性别",
              prop: "sex",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=sex",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入当事人性别",
                trigger: "blur"
              }],
            },
            {
              label: "当事人民族",
              prop: "nation",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=nation",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入当事人民族",
                trigger: "blur"
              }],
            },
             {
              label: "当事人学历",
              prop: "education",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=education",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入学历",
                trigger: "blur"
              }],
            },
            // {
            //   label: "当事人类别",
            //   prop: "personnelCategory",
            //   type: "select",
            //   dicUrl: "/api/smart-system/dict/dictionary?code=personnelCategory",
            //   props: {
            //     label: "dictValue",
            //     value: "dictKey"
            //   },
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入当事人人员类别",
            //     trigger: "blur"
            //   }],
            // },
            {
            label: '当事人类别',
            prop: 'personnelCategory',
            type: "cascader",
            dicData: [],
            filterable: true,
            hide: true,
            rules: [{
              required: true,
              message: "请输入当事人人员类别",
              trigger: "blur"
            }],
            },
            {
              label: "当事人居住详址",
              prop: "detailAddress",
              type: "input",
              hide: true,
              span: 24,
              rules: [{
                required: true,
                message: "请输入主要人居住详址",
                trigger: "blur"
              }],
            },
            // {
            //   label: "办理方式",
            //   prop: "handleType",
            //   type: "select",
            //   dicData: [
            //   {
            //     label: "上报社区村",
            //     value: "community"
            //   },
            //   {
            //     label: "上报镇街",
            //     value: "streetCenter"
            //   },
            //   {
            //     label: "自办",
            //     value: "01"
            //   }
            // ],
            //   hide: true,
              
            // },
            {
              label: "化解时限",
              prop: "resolvingTimeLimits",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "化解方式",
              prop: "waysOfResolving",
              type: "input",
              hide: true,
              display: false,
            },
            {
              label: "化解组织",
              prop: "dissolveTheOrg",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
              
            },
            {
              label: "化解责任人姓名",
              prop: "nameOfDefuser",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              search: true,
            },
            {
              label: "化解责任人电话",
              prop: "contactOfDefuser",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "化解是否成功",
              prop: "successfulOrNot",
              type: "input",
              checkStrictly: true,
               multiple: true,
              dicData: [{
                    label: "否",
                    value: "0"
              },{
                    label: "是",
                    value: "1"
              }],
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "化解情况",
              prop: "resolveTheSituation",
              type: "input",
              hide: true,
              display: false,
            },
            //  {
            //   label: "考评日期",
            //   prop: "appraisalDate",
            //   type: "date",
            //   format: "yyyy-MM-dd",
            //   valueFormat: "yyyy-MM-dd",
            //    display: false,
            // },
            // {
            //   label: "考评意见",
            //   prop: "reviewOpinions",
            //   type: "input",
            //   hide: true,
            //   display: false,
            // },
            {
              label: "经度",
              prop: "lng",
              type: "input",
              hide: true,
            },
            {
              label: "纬度",
              prop: "lat",
              type: "input",
              hide: true,
            },
            // {
            //   label: "处理意见",
            //   prop: "handlingSuggestion",
            //   type: "textarea",
            //   minRows:1,
            //   maxlength: 4000,
            //   showWordLimit: true,
            //   hide: true,
            //   span: 24,
            // },
            {
              label: "催办次数",
              prop: "urgeCount",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "指派单位",
              prop: "turnDept",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "下一步骤流程对应的id",
              prop: "nextFlowId",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "备注",
              prop: "remark",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            
          ]
        },
        data: []
      };
    },
    watch:{
      //选择处理方式时显示和隐藏元素
        // 'form.handleType': {
        //   handler(val){
        //     var waysOfResolving = this.findObject(this.option.column ,"waysOfResolving");
        //     var resolveTheSituation = this.findObject(this.option.column,"resolveTheSituation");
        //     var appraisalDate  = this.findObject(this.option.column , "appraisalDate");
        //     var reviewOpinions = this.findObject(this.option.column,"reviewOpinions");
        //     if(val == '01') {
        //         waysOfResolving.display= true;
        //         resolveTheSituation.display = true;
        //         appraisalDate.display = true;
        //         reviewOpinions.display = true;
        //     }else{
        //         waysOfResolving.display= false;
        //         resolveTheSituation.display = false;
        //         appraisalDate.display = false;
        //         reviewOpinions.display = false;
        //     }

        //   }
        // }
 
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.conflictsResolve_add, false),
          viewBtn: this.vaildData(this.permission.conflictsResolve_view, false),
          delBtn: this.vaildData(this.permission.conflictsResolve_delete, false),
          editBtn: this.vaildData(this.permission.conflictsResolve_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
     mounted() {
        this.initData();
    },
    methods: {
       initData() {
         console.log("11111111111111111111111111111111111111111111");
       let dictType1 = "event_type";
        getMultilevelMore(dictType1).then(res=>{
          var eventCategory =this.findObject(this.option.column,'eventCategory');
          console.log(res.data.data);
          eventCategory.dicData = res.data.data;
        });
      let dictType2 = "personnel_category";
      getMultilevelMore(dictType2).then(res=>{
          var personnelCategory =this.findObject(this.option.column,'personnelCategory');
          console.log(res.data.data);
          personnelCategory.dicData = res.data.data;
        });
      },
      rowSave(row, done, loading) {
        console.log(row.personnelCategory);
        row.personnelCategory = row.personnelCategory.join(",");
        row.eventCategory = row.eventCategory.join(",");
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.personnelCategory = row.personnelCategory.join(",");
        row.eventCategory = row.eventCategory.join(",");
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
         if (["add"].includes(type)) {
          setTimeout(() => {
            //新增时添加默认值
            this.form.handleType = 'community',
            this.form.sex = '1';
            this.form.nation = '01';
            this.form.identificationCode = '01';
          }, 10);
        }
        
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
           //this.form = res.data.data;
             if(this.form.hasOwnProperty("personnelCategory")){
              this.form.personnelCategory = res.data.data.personnelCategory.split(",");
            }
             if(this.form.hasOwnProperty("eventCategory")){
              this.form.eventCategory = res.data.data.eventCategory.split(",");
            }
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.el-form-item__content{
   margin-left: 130px ! important;
}
.el-form-item__label {
  width: 130px ! important;
}
</style>
